package 贪心;

import java.util.Arrays;

/**
 * @ClassName _1005K次取反后最大化的数组和
 * @Description TODO
 * @Author 昝亚杰
 * @Date 2021/12/20 19:53
 * Version 1.0
 **/
public class _1005K次取反后最大化的数组和 {
    public int largestSumAfterKNegations(int[] nums, int k) {
        int count = 0;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (count >= k) {
                break;
            }
            if (nums[i] < 0) {
                nums[i] = -nums[i];
                count++;
            } else {
                Arrays.sort(nums);
                nums[0] = -nums[0];
                count++;
            }
            i = i%(nums.length - 1);
            System.out.println(i);
        }
        return sum(nums);
    }

    private int sum(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        return sum;
    }
}
